AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include('shared.lua')

local DrillAmount = { easy = 50 , medium = 20 , hard = 10 , tough = 2 }

function ENT:Initialize()
	self.BaseClass.Initialize(self)
	self.resources = {}
	self.vein = true
	self.linked = false
	self.totalResources = 0
	--self.detected = true
	self.detected = false
	--self.detectedto = CurTime() + 120
	self.detectedto = 0
	self.maxcap = 0
	self.lifetime = 0
	self.hitnormal = Vector(0,0,0)
	self.spawnpos = Vector(0,0,0)
end

function ENT:AddResource(res)
	res.beenmined = false
	table.insert(self.resources,res)
end

function ENT:Dig(multiplier)
	local minedsome = false
	local SM = CAF.GetAddon("Selling Machine")
	for k,v in ipairs(self.resources) do
		if (v.amount > 0) then
			minedsome = true
			local curmin = SM.gResources[v.name]
			local randmult = math.Rand(0.2,1.8)
			multiplier = multiplier * randmult 
			local mined = 0
			local beenmined = true
			self.resources[k].amount = self.resources[k].amount - math.floor(DrillAmount[curmin.type] * multiplier)
			if (self.resources[k].amount < 0) then
				mined = math.floor(DrillAmount[curmin.type] * multiplier) + self.resources[k].amount
				self.resources[k].amount = 0
			else
				mined = math.floor(DrillAmount[curmin.type] * multiplier)
			end
			if not v.beenmined then
				beenmined = false
				self.resources[k].beenmined = true
			end
			self.totalResources = self.totalResources - mined
			return true, {name=v.name,amount=mined,bm=beenmined,veinsize=v.veinsize}
		end
	end
	SM.RemoveVein(self)
	return false
end

function ENT:Detected(nSec)
	self.detected = true
	self.lifetime = CurTime() + (server_settings.Int("SM_respawn_maxlife")*60)
	self.detectedto = CurTime() + nSec
	MsgN("Detected: " .. self:EntIndex())
end

function ENT:TriggerInput(iname, value)

end

function ENT:OnRemove()
	self:Remove()
end

function ENT:Think()
	--if self.totalResources < 1 then
	--	CAF.GetAddon("Selling Machine").RemoveVein(self)
	if CurTime() > self.lifetime then
		--check if it was detected. If so, extend its life.
		--Otherwise, remove it. Obviously nobody cared for the poor thing anyway. :(
		if self.detected or self.linked then
			self.lifetime = CurTime() + (server_settings.Int("SM_respawn_maxlife")*60)
		else
			CAF.GetAddon("Selling Machine").RemoveVein(self)
		end
	end
	if (CurTime() > self.detectedto) then
		self.detected = false
		self.detectedto = 0
	end
	--check if the asteroid was moved (exploited)
	if self:GetPos() != self.spawnpos then
		self:SetPos(self.spawnpos)
	end
	if self.detected then
		local Smoke = ents.Create("env_smoketrail")
		Smoke:SetKeyValue("opacity", 1)
		Smoke:SetKeyValue("spawnrate", 10)
		Smoke:SetKeyValue("lifetime", 5)
		Smoke:SetKeyValue("startcolor", "20 180 20")
		Smoke:SetKeyValue("endcolor", "80 255 80")
		Smoke:SetKeyValue("minspeed", 15)
		Smoke:SetKeyValue("maxspeed", 30)
		Smoke:SetKeyValue("startsize", 10)
		Smoke:SetKeyValue("endsize", 50)
		Smoke:SetKeyValue("spawnradius", 10)
		Smoke:SetKeyValue("emittime", 300)
		Smoke:SetKeyValue("firesprite", "sprites/firetrail.spr")
		Smoke:SetKeyValue("smokesprite", "sprites/whitepuff.spr")
		Smoke:SetPos(self.Entity:GetPos())
		Smoke:SetParent(self.Entity)
		Smoke:Spawn()
		Smoke:Activate()
		Smoke:Fire("kill","", 1)
	end
	self.Entity:NextThink( CurTime() + 1)
	return true
end

function ENT:CanTool()
	return false
end

function ENT:GravGunPunt()
	return false
end

function ENT:GravGunPickupAllowed()
	return false
end
